package org.shj.thread.javaconcurrencypattern.guardsuspension;

import org.shj.thread.javaconcurrencypattern.common.AlarmAgent;
import org.shj.thread.javaconcurrencypattern.common.AlarmInfo;
import org.shj.thread.javaconcurrencypattern.twophasetermination.AlarmType;
import org.shj.thread.util.Util;

public class Main {

	public static void main(String[] args){
		final AlarmAgent alarmAgent = new AlarmAgent();
		
		for(int i = 0; i < 10; i++){
			String msg = "Message" + i;
			final AlarmInfo info = new AlarmInfo(msg, AlarmType.RESUME);
			
			Runnable task = new Runnable(){
				public void run(){
					try {
						alarmAgent.sendAlarm(info);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			};
			new Thread(task).start();
		}
		
		//让发送消息的线程启动起来，此时尚未初始化，即尚未与服务器建立连接，以模拟发送线程需等待，直到alarmAgent完成初始化
		Util.sleep(2); 
		
		alarmAgent.init();
	}
}
